package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.ssm.base.BaseController;
import com.ssm.entity.Role;
import com.ssm.service.PermissionService;
import com.ssm.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/13
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    private static final String SUCCESS_PAGE="common/successPage";
    @Reference
    private RoleService roleService;
    @Reference
    private PermissionService permissionService;

    /**
     * 跳转到role/index.html
     * @param model
     * @param request
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.show')")
    @RequestMapping
    public String index(ModelMap model,HttpServletRequest request){
//        List<Role> roleList = roleService.findAll();
//        map.put("list",roleList);
        //获取请求参数
        Map<String, Object> filters = getFilters(request);
        //
        PageInfo<Role> page = roleService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "role/index";
    }

    /**
     * 跳转到role/create.html
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.create')")
    @GetMapping("/create")
    public String insert(){
        return "role/create";
    }

    /**
     * 保存role
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.create')")
    @RequestMapping("/save")
    public String saveRole(Role role, HttpServletRequest request){
        roleService.insert(role);
        //重定向到查询页面
        //return "redirect:/role";
        return SUCCESS_PAGE;
    }

    /**
     * 根据id获取role信息
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.edit')")
    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable long id, ModelMap model){
        Role roleById = roleService.getById(id);
        model.addAttribute("role",roleById);
        return "role/update";
    }

    /**
     * 保存修改role信息
     * @param role
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.edit')")
    @PostMapping("/update")
    public String update(Role role){
        roleService.update(role);
        return SUCCESS_PAGE;
    }

    /**
     * 根据id删除role
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.delete')")
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        roleService.deleteById(id);
        return "redirect:/role";
    }

    /**
     * 进入分配权限页面
     * @param map
     * @param roleId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.assignShow')")
    @GetMapping("/assignShow/{roleId}")
    public String assignShow(Map map,@PathVariable("roleId") Long roleId){
        List<Map<String,Object>> zNodes = permissionService.findPermissionByRoled(roleId);
        map.put("zNodes",zNodes);
        map.put("roleId",roleId);
        return "role/assignShow";
    }

    /**
     * 给角色分配权限
     * @param roleId
     * @param permissionIds
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.assignShow')")
    @PostMapping("/assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);
        return SUCCESS_PAGE;
    }
}
